<template>
  <TopMenu :logo="eventLogo" />
  <div class="bodyContainer">
    <ImageCarousel :images="basicImages" :showArrows="false" height="500px" />
    <div class="endTime-container">
      <!--  倒计时    -->
      <h1 class="mainTitle">2025深圳马拉松</h1>
      <h1 class="subTitle">距离比赛日</h1>
      <CountDown :targetTime="targetTime" :onFinish="handleCountdownFinish" />
      <!-- 二级导航-->
      <div class="container subMenu">
        <div class="menuBtn" @click="router.push('/signUp')">
          <div class="img-box">
            <ImageLoad filePath="baoming.png" class="menuIcon"></ImageLoad>
          </div>
          <p class="_name">报名</p>
        </div>
        <div class="menuBtn" v-for="(item, index) in eventContents"
             :key="`menuBtn_${index}`"
             @click="router.push(`/eventDetails?type=${index}`)">
          <div class="img-box">
            <ImageLoad :filePath="item.icon" class="menuIcon"></ImageLoad>
          </div>
          <p class="_name">{{item['themeTitle']}}</p>
        </div>
      </div>

      <!--    赛事公告  -->
      <div class="container content-wrapper">
        <div class="content-title">
          <ImageLoad filePath="titleIcon.png" class="titleIcon"></ImageLoad>
          <span>赛事公告</span>
          <span class="more" @click="seeMore(1)">查看更多 <SvgLoad name="arrowRight" class="arrow"></SvgLoad></span>
        </div>
        <!--    公告内容    -->
        <div class="box-wrapper">
          <div class="content-box" v-for="(item, index) in postList" :key="index" @click="seeMoreDetails(1,item.id)">
            <div class="gonggao-date">
              <div class="day">{{item.date.split('-')[2]}}/</div>
              <div class="year">
                <p>{{item.date.split('-')[1]}}月</p>
                <p>{{item.date.split('-')[0]}}</p>
              </div>
            </div>
            <div class="divider"></div>
            <p class="gonggao-title">【{{ item.title }}】</p>
          </div>
        </div>
      </div>
      <!--    赛事新闻  -->
      <div class="container content-wrapper">
        <div class="content-title">
          <ImageLoad filePath="titleIcon.png" class="titleIcon"></ImageLoad>
          <span>赛事新闻</span>
          <span class="more" @click="seeMore(2)">查看更多 <SvgLoad name="arrowRight" class="arrow"></SvgLoad></span>
        </div>
        <!--    新闻内容    -->
        <div class="news-wrapper">
          <div class="news-box" v-for="(item, index) in newsList" :key="`news_${index}`" @click="seeMoreDetails(2,item.id)">
            <img :src="item.image" />
            <div class="_title">{{item.title}}</div>
            <div class="category">{{item.createDate}}</div>
          </div>
        </div>
      </div>
      <!--    精彩图集  -->
      <div class="container content-wrapper">
        <div class="content-title">
          <ImageLoad filePath="titleIcon.png" class="titleIcon"></ImageLoad>
          <span>精彩图集</span>
          <span class="more" @click="seeMore(3)">查看更多 <SvgLoad name="arrowRight" class="arrow"></SvgLoad></span>
        </div>
        <!--    图集内容    -->
        <div class="album-wrapper">
          <div class="img-box" v-for="(item ,index) in albumsList" :key="`${index}_album`">
            <img :src="item.image" alt="" />
          </div>

        </div>
      </div>
      <!--     品牌走马灯 -->
      <div class="container brandWrap">
        <BrandCarousel :brands="brandList" :visibleItems="7" @click="handleBrandClick" />
      </div>
    </div>
  </div>
</template>

<script setup>
import TopMenu from '@/components/TopMenu/index.vue'
import ImageCarousel from '@/components/common/Swiper/ImageCarousel.vue'
import CountDown from '@/components/common/CountDown/index.vue'
import ImageLoad from '@/components/common/ImageLoad/index.vue'
import SvgLoad from '@/components/common/SvgLoad/index.vue'
import BrandCarousel from '@/components/common/BrandCarousel/index.vue'
import { ref } from 'vue'
import { eventData } from '@/api/data.js'
import { useRoute, useRouter } from 'vue-router'
import { useEventStore } from '@/stores/event/index.js'
const $route = useRoute()
const router = useRouter()
const eventStore = useEventStore()
// 倒计时目标时间（默认为当前时间 + 1天）
const targetTime = ref(new Date(Date.now() + 240 * 60 * 60 * 1000).toISOString())
const isFinished = ref(false)
// 品牌列表数据
const brandList = ref([])
// 公告内容
const postList = ref([])
// 赛事内容
const eventContents = ref([]);
// 新闻内容
const newsList = ref([]);
// 精彩图集
const albumsList = ref([]);
// 基础图片数据
const basicImages = ref([])
// 赛事logo
const eventLogo = ref('')
// 倒计时结束的回调函数
const handleCountdownFinish = () => {
  isFinished.value = true
  console.log('倒计时结束！')
}

const seeMore = (type) => {
  router.push({
    path: '/seeMore',
    query: {
      type: type,
    }
  });
}
const seeMoreDetails = (type,id) => {
  router.push({
    path: '/seeMore',
    query: {
      type: type,
      id: id,
    }
  });
}
// 品牌点击事件处理
const handleBrandClick = (data) => {
  console.log('品牌点击:', data)
  if (data.brand.link) {
    window.open(data.brand.link, '_blank')
  }
}

const formatDate = (dateString)=> {
  const date = new Date(dateString);

  // 使用toISOString获取标准格式，然后进行调整
  return date.toISOString()
    .replace('T', ' ')
    .substring(0, 16);
}
const getEventData = () => {
  const eventId = $route.query.evtId || 5
  eventData(eventId).then((res) => {
    if (res.code == 200) {
      targetTime.value = formatDate(res.data.evt.beginTime);
      eventContents.value = res.data.evt.contents;
      basicImages.value = res.data.evt.bannersStr.split(',');
      postList.value = res.data.contentPosts.map(item => {
        return {
          ...item,
          date: item.createDate.split(' ')[0]
        }
      });
      newsList.value = res.data.news;
      albumsList.value = res.data.albums;
      brandList.value = res.data.partners;
      eventLogo.value = res.data.evt.logo;
      eventStore.setCurrentEventData(res.data.evt);
    }
  })
}
onMounted(() => {
  getEventData()
})
</script>

<style scoped lang="scss">
.endTime-container {
  background-image: url('/public/images/bannerBg.png');
  min-height: 320px;
  background-size: cover;
  background-repeat: no-repeat;
  text-align: center;
  .mainTitle {
    padding-top: 50px;
    text-align: center;
    padding-bottom: 20px;
    font-size: 68px;
    font-weight: bold;
    line-height: 68px;
    color: #333333;
  }
  .subTitle {
    font-size: 28px;
    font-weight: 400;
    line-height: 28px;
    text-align: center;
    padding-bottom: 20px;
    color: #333333;
  }
}
.divider {
  height: 60px;
  width: 2px;
  background: #eeeeee;
}
.subMenu {
  display: flex;
  gap: 15px;
  align-items: center;
  flex-wrap: wrap;
  padding: 80px 0;
  justify-content: center;
  .menuBtn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: 4px solid #ffffff;
    width: 172px;
    height: 152px;
    border-radius: 16px;
    font-size: 18px;
    color: #333333;
    background: #EDEEF4;
    cursor: pointer;
    .img-box {
      background: #ffffff;
      width: 66px;
      height: 66px;
      border-radius: 16px;
      padding: 12px;
      img {
      }
    }
    ._name {
      margin-top: 20px;
      font-size: 18px;
    }
    &:hover {

      color: #5F3789;
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
      transform: scale(1.05);
      transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275),
      box-shadow 0.5s ease,
      filter 0.5s ease;
    }
  }
}
.content-wrapper {
  .content-title {
    display: flex;
    align-items: center;
    gap: 18px;
    padding-bottom: 51px;
    span {
      font-size: 26px;
      font-family: PingFangSC-Bold;
      font-weight: bold;
      color: #5f3789;
    }
    .titleIcon {
      width: 40px;
    }
    .more {
      margin-left: auto;
      display: flex;
      align-items: center;
      font-size: 16px;
      cursor: pointer;
      color: #5f3789;
      .arrow {
        font-size: 14px;
      }
    }
  }
}
.box-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  padding-bottom: 80px;
  .content-box {
    background: #ffffff;
    border-radius: 12px;
    padding: 30px 12px;
    flex: 1;
    display: flex;
    align-items: center;
    gap: 12px;

    cursor: pointer;
    &:hover {
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
      transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275),
      box-shadow 0.5s ease,
      filter 0.5s ease;
      transform: scale(1.01);
    }
    .gonggao-date {
      display: flex;
      align-items: center;
      .day {
        font-size: 34px;
        color: #333333;
        padding-right: 5px;
      }
      .year {
        font-size: 18px;
        color: #787878;
      }
    }
  }
}
.news-wrapper {
  display: flex;
  //align-items: center;
  gap: 24px;
  padding-bottom: 40px;
  flex-wrap: wrap;
  .news-box {
    background: #ffffff;
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 295px;
    cursor: pointer;
    &:hover {
      box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
      transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275),
      box-shadow 0.5s ease,
      filter 0.5s ease;
      transform: scale(1.01);
    }
    img {
      height: 180px;
      border-radius: 20px 20px 0 0;
      object-fit: cover;
    }
    ._title {
      font-size: 16px;
      padding: 16px;
      text-align: left;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
      height:55px;
    }
    .category {
      margin-top: auto;
      font-size: 18px;
      padding: 20px 20px 27px;
      text-align: left;
      color: #787878;
    }
  }
}
.album-wrapper {
  display: flex;
  align-items: center;
  gap: 24px;
  padding-bottom: 40px;
  flex-wrap: wrap;
  .img-box {
    //flex: 1;
    display: flex;
    width: 400px;
    img {
      width: 100%;
      height: 260px;
      border-radius: 20px;
      object-fit: cover;
    }
  }
}
.brandWrap {
  padding: 40px 0;
}
</style>
